// pages/operat/codemanager/codemanager.js
var LApp = getApp();

import _global from "../../../source/js/global"



Page({
  /**
   * 页面的初始数据
   */
  data: {
    genCodeList: [],
    pageIndex: 1,
    searchKey: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.genCodeList = new Array();
    this.getGenCodeList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      open:-1
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.genCodeList = new Array();
    this.getGenCodeList();
    wx.stopPullDownRefresh();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getGenCodeList(this.data.pageIndex += 1);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      path: '/page/user?id=123',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  bindSearch: function (e) {
    this.setData({
      searchKey: e.detail.value
    })
    this.genCodeList = new Array();
    this.getGenCodeList();
  },
  getGenCodeList: function (pageindex) {
    var _this = this;

    this.data.pageIndex = pageindex || 1;
    LApp._get({
      url: "nbapi/codemanager/gencode/list",
      data: { pageIndex: _this.data.pageIndex, pageSize: 15, Rsearch: _this.data.searchKey },
      success: function (r) {
        _this.setData({
          genCodeList: _this.genCodeList.appendArray(r)
        })
      }
    })
  },
  /**
   * 右滑操作 Begin
  */
  touchMove: _global.Tap_Dray,
  tap_end: _global.Tap_End,
   /**
   * 右滑操作 End
  */
  bindViewClick:function(e){
    this.codeModel = e.currentTarget.dataset.codemodel;
    if (this.codeModel) {
      LApp.pages.add(this);
      wx.navigateTo({
        url: 'codeAddOrEdit/codeAddOrEdit?isEdit=false',
      })
    }
  },
  /**
   * 编辑操作
   */
  bindEditDetailClick:function(e){
    this.codeModel = e.currentTarget.dataset.codemodel;
    if (this.codeModel){
      LApp.pages.add(this);
      wx.navigateTo({
        url: 'codeAddOrEdit/codeAddOrEdit',
      })
    }
  },
   /**
   * 码申请
   */
  goCodeApply:function(){
    wx.navigateTo({
      url: 'codeApply/codeApply',
    })
  },
  goCodeAllot:function(){

   if (LApp.globalData.userInfo.HeadId == 1 ) {
    wx.navigateTo({
     url: 'codeAllot/CodeAllotList/CodeAllotList',
    })
   }
   else if (LApp.globalData.userInfo.EnterpriseId == 1)
   {
    wx.showModal({
     title: '提示',
     content: "当前机构暂无分配信息，请在查看生码信息！",
     showCancel: false
    })
    return;
   }
   else
   {
    wx.showModal({
     title: '提示',
     content: "您没有码分配权限！",
     showCancel: false
    })
    return;
   }
  }
})